<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='global-property-'>/**
</span> * @ignore
 * represent line index of each line
 * @author yiminghe@gmail.com
 */
KISSY.add(&quot;html-parser/lexer/index&quot;, function () {

<span id='KISSY-HtmlParser-Lexer-Index'>    /**
</span>     * Page index class.
     * @private
     * @class KISSY.HtmlParser.Lexer.Index
     */
    function Index() {
        this.lineCursors = [];
    }

    Index.prototype = {
        constructor: Index,

        add: function (cursor) {
            if (indexOfCursor(this.lineCursors, cursor) != -1) {
                return;
            }
            var index = indexOfCursorForInsert(this.lineCursors, cursor);
            this.lineCursors.splice(index, 0, cursor);
        },

        remove: function (cursor) {
            var cs = this.lineCursors;
            var index = indexOfCursor(this.lineCursors, cursor);
            if (index != -1) {
                cs.splice(index, 1);
            }
        },

<span id='KISSY-HtmlParser-Lexer-Index-method-row'>        /**
</span>         * line number of this cursor , index from zero
         * @param cursor
         */
        row: function (cursor) {
            var cs = this.lineCursors;
            for (var i = 0; i &lt; cs.length; i++) {
                if (cs[i].position &gt; cursor.position) {
                    return i - 1;
                }
            }
            return i;
        },

        col: function (cursor) {
            var linePosition = 0,
                lineCursor;
            if (lineCursor = this.lineCursors[this.row(cursor) - 1]) {
                linePosition = lineCursor.position;
            }
            return cursor.position - linePosition;
        }
    };

    function indexOfCursor(cs, c) {
        for (var i = 0; i &lt; cs.length; i++) {
            if (cs[i].position === c.position) {
                return i;
            }
        }
        return -1;
    }

    function indexOfCursorForInsert(cs, c) {
        for (var i = 0; i &lt; cs.length; i++) {
            if (cs[i].position &gt; c.position) {
                return i;
            }
        }
        return i;
    }

    return Index;

});</pre>
</body>
</html>
